import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:message_tools/application.dart';

class UpdaterPage extends StatefulWidget {
  final Widget child;

  const UpdaterPage(this.child);

  @override
  UpdatePagerState createState() => UpdatePagerState();
}

class UpdatePagerState extends State<UpdaterPage> {
  int _serviceVersionCode;
  BuildContext _context;

  @override
  void initState() {
    super.initState();

    Future.delayed(Duration(seconds: 5)).then((_) {
      _getNewVersionAPP();
    });
  }

  //执行版本更新的网络请求
  _getNewVersionAPP() async {
    String url = "http://47.102.217.170/v1/open/get_version";
    Dio().get(url).then((response) {
      if (response != null) {
        setState(() {
          var data = response.data["data"];
          debugPrint("data01: $data \n");
          debugPrint("data02: ${data["version"]} \n");
          _serviceVersionCode = int.parse(data["version"].toString());
          _checkVersionCode();
        });
      }
    });
  }

  //检查版本更新的版本号
  _checkVersionCode() async {
    int currentVersionCode = await Application.getInt("version") ?? 1;
    if (_serviceVersionCode > currentVersionCode) {
      //_showNewVersionAppDialog();
      mAlertDialog(_context, "更新内容：1, 2, 3...");
    }
  }

  void mAlertDialog(BuildContext context, String content) {
    showDialog(
      context: context,
      barrierDismissible: true, // user must tap button!
      builder: (BuildContext context) {
        return AlertDialog(
          title: Text('系统提示'),
          content: Text(content),
          actions: <Widget>[
            FlatButton(
              child: Text('确定'),
              onPressed: () {
                Navigator.of(context).pop();
              },
            ),
          ],
          elevation: 20,
          // 设置成 圆角
          shape:
              RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
        );
      },
    );
  }

  //弹出"版本更新"对话框
  _showNewVersionAppDialog() async {
    return showDialog<void>(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return AlertDialog(
            title: new Row(
              children: <Widget>[
                new Image.asset("images/download.png",
                    height: 35.0, width: 35.0),
                new Padding(
                    padding: const EdgeInsets.fromLTRB(30.0, 0.0, 10.0, 0.0),
                    child:
                        new Text("标题", style: TextStyle(color: Colors.black)))
              ],
            ),
            content:
                new Text("修复内容1，2，3", style: TextStyle(color: Colors.black)),
            actions: <Widget>[
              new FlatButton(
                child: new Text("取消", style: TextStyle(color: Colors.blue)),
                onPressed: () {
                  Navigator.of(context).pop();
                },
              ),
              new FlatButton(
                child: new Text("开始下载", style: TextStyle(color: Colors.red)),
                onPressed: () {
//                launch("https://play.google.com/store/apps/details?id=项目包名");
                  //_launchGooglePlay();
                  Navigator.of(context).pop();
                },
              )
            ],
          );
        });
  }

  @override
  Widget build(BuildContext context) {
    _context = context;

    debugPrint("print ===============\n");

    return widget.child;
  }
}
